package com.xudow.app.dynamicstate_old.module.recommend;

import android.os.Bundle;
import android.support.annotation.NonNull;

import com.jude.beam.expansion.list.BeamListFragmentPresenter;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;

/**
 * Created by ske on 2016/6/11.
 */
public class RecommendPresenter extends BeamListFragmentPresenter<RecommendFragment, Dynamic> {
    long lastTime = 0;

    @Override
    protected void onCreate(@NonNull RecommendFragment view, Bundle savedState) {
        super.onCreate(view, savedState);
        onRefresh();
    }

    @Override
    public void onRefresh() {
            DynamicModel.getInstance()
                    .recommentDynamics(10, lastTime, 1)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getPublishTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getRefreshSubscriber());
    }

    @Override
    public void onLoadMore() {
            DynamicModel.getInstance()
                    .recommentDynamics(10, lastTime, 2)
                    .doOnNext(list -> {
                        if (list!=null&&list.size()>0)
                            lastTime = list.get(list.size()-1).getPublishTime();
                    })
                    .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                    .unsafeSubscribe(getMoreSubscriber());

    }
}